#!/bin/bash


BinHome="/opt/server.lan"
MntHome="/mnt/server.lan"
TmpHome="/tmp/server.lan"


docker run --pull always --rm -v "$BinHome/ssl":/data inthbl/ssl-gen \
    CN   zhejiang   Yh   SmallGroupX   dev   "server.lan" \
    'server.lan *.server.lan git.lan *.git.lan'


# gen_req_conf  CN   zhejiang   Yh   SmallGroupX   dev   "server.lan"
# SAN='server.lan *.server.lan git.lan *.git.lan'


(
    status=on
    cat > "$BinHome/settings_and_scripts/cmd/gpio_$status.sh" <<EOF
#!/bin/bash

flock -w 5 "$TmpHome/gpio_$status.lock" -c 'bash "$BinHome/gpio.sh" "$status" > "$TmpHome/gpio_$status.log"'

EOF

    status=off
    cat > "$BinHome/settings_and_scripts/cmd/gpio_$status.sh" <<EOF
#!/bin/bash

flock -w 5 "$TmpHome/gpio_$status.lock" -c 'bash "$BinHome/gpio.sh" "$status" > "$TmpHome/gpio_$status.log"'

EOF

)

(
    # only restart

    for name in server.lan seafile.server.lan git.lan bitwarden.server.lan ;do

        cat > "$BinHome/settings_and_scripts/cmd/docker_container_restart_$name.sh" <<EOF
#!/bin/bash

flock -w 10 "$TmpHome/docker_container_restart_$name.lock" -c "docker restart $name >> $TmpHome/docker_container_restart"

EOF
done

)

(
    # both start and stop

    for name in fileserver_wan ;do

        # restart
        cat > "$BinHome/settings_and_scripts/cmd/docker_container_restart_$name.sh" <<EOF
#!/bin/bash

flock -w 10 "$TmpHome/docker_container_restart_$name.lock" -c "docker restart $name >> $TmpHome/docker_container_restart"

EOF

        # stop
        cat > "$BinHome/settings_and_scripts/cmd/docker_container_stop_$name.sh" <<EOF
#!/bin/bash

flock -w 10 "$TmpHome/docker_container_stop_$name.lock" -c "docker stop $name >> $TmpHome/docker_container_stop"

EOF
done

)
